﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetaData;
using System.IO;
using System.Threading;
using Common;

namespace MetaEditor
{
    public partial class MetaEditor : Form
    {
        MetaDataBag dataBag;
        PropertyGrid pg;
        AppWindow table;
        public MetaEditor()
        {

            InitializeComponent();

            dataBag = MetaDataBag.Load();
            //GetBoardImagesHash();
            //GetHoleImagesHash();
            GetPotNumbers();
            //filePaths = Directory.GetFiles(@"c:\MyProject\DotNetTest\MetaEditor\bets", "*.bmp");
            //foreach (string path in filePaths)
            //{
            //    FileInfo finfo = new FileInfo(path);
            //    string value = finfo.Name.Split('.')[0];
            //    Bitmap bmp = (Bitmap)Bitmap.FromFile(path);
            //    int key = Utility.GetFrontHash(bmp, new Rectangle(new Point(0, 0), bmp.Size), dataBag.BetFontColor);
            //    if (!dataBag.BetNumberHashes.ContainsKey(key))
            //    {
            //        dataBag.BetNumberHashes.Add(key, value);
            //    }
            //}
            pg = new PropertyGrid();
            pg.Dock = DockStyle.Fill;
            editorPanel.Controls.Add(pg);
            pg.SelectedObject = dataBag;
        }

        private void GetPotNumbers()
        {
            string[] filePaths = Directory.GetFiles(@"C:\DotNetCSharp\MetaEditor\bin\Debug\images\potnumbers", "*.bmp");
            dataBag.PotNumberHashes = new Dictionary<long, string>();
            foreach (string path in filePaths)
            {
                FileInfo finfo = new FileInfo(path);
                string value = finfo.Name.Split('.')[0];
                Bitmap bmp = (Bitmap)Bitmap.FromFile(path);
                long key = Utility.GetFrontHash(bmp, new Rectangle(new Point(0, 0), bmp.Size), dataBag.PotFontColor);
                if (!dataBag.PotNumberHashes.ContainsKey(key))
                {
                    dataBag.PotNumberHashes.Add(key, value);
                }
            }
        }

        private void GetBoardImagesHash()
        {
            dataBag.BoardCardHashes = new Dictionary<int, string>();
            string[] filePaths = Directory.GetFiles(@"C:\DotNetCSharp\MetaEditor\bin\Debug\images", "*.bmp");
            foreach (string path in filePaths)
            {
                FileInfo finfo = new FileInfo(path);
                string value = finfo.Name.Split('.')[0];
                Bitmap bmp = (Bitmap)Bitmap.FromFile(path);
                int key = Utility.GetRegionHash(bmp, new Rectangle(new Point(0, 0), bmp.Size));

                if (!dataBag.BoardCardHashes.ContainsKey(key))
                {
                    dataBag.BoardCardHashes.Add(key, value);
                }
            }
        }
        private void GetHoleImagesHash()
        {
            dataBag.HoleCardHashes = new Dictionary<int, string>();
            string[] filePaths = Directory.GetFiles(@"C:\DotNetCSharp\MetaEditor\bin\Debug\images\HoleCards", "*.bmp");
            foreach (string path in filePaths)
            {
                FileInfo finfo = new FileInfo(path);
                string value = finfo.Name.Split('.')[0];
                Bitmap bmp = (Bitmap)Bitmap.FromFile(path);
                int key = Utility.GetRegionHash(bmp, new Rectangle(new Point(0, 0), bmp.Size));

                if (!dataBag.HoleCardHashes.ContainsKey(key))
                {
                    dataBag.HoleCardHashes.Add(key, value);
                }
            }
        }
        private void SaveBtn_Click(object sender, EventArgs e)
        {
           dataBag.Save();
        }

        private void GetCardImages_Click(object sender, EventArgs e)
        {
            if (dataBag.BoardCardSize.Width == 0)
            {
                MessageBox.Show("Please enter meta first!");
                return;
            }
            if (dataBag.BoardCardHashes == null)
            {
                dataBag.BoardCardHashes = new Dictionary<int, string>();
            }
            while (dataBag.BoardCardHashes.Count < 320)
            {
                Bitmap bmp = Utility.CaptureWindow(table);
                for (int i = 0; i < 5; i++)
                {
                    Bitmap card = Utility.GetRegion(bmp, new Rectangle(dataBag.CommonCards[i], dataBag.BoardCardSize));
                    int key = Utility.GetRegionHash(card, new Rectangle(new Point(0, 0), card.Size));
                    if (!dataBag.BoardCardHashes.ContainsKey(key))
                    {
                        dataBag.BoardCardHashes.Add(key, string.Empty);
                        if (!Directory.Exists(Directory.GetCurrentDirectory() + "\\images"))
                        {
                            Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\images");
                        }
                        string fileName = Directory.GetCurrentDirectory() + "\\images\\" + DateTime.Now.Ticks.ToString() + ".bmp";
                        card.Save(fileName);
                    }
                }
                lblStatus.Text = dataBag.BoardCardHashes.Count.ToString() + " of 57 completed";
                Thread.Sleep(5000);
            }
            lblStatus.Text = "Image creation finished!";
        }

        private void BtnSetFold_Click(object sender, EventArgs e)
        {
            Bitmap bmp = Utility.CaptureWindow(table);
            dataBag.RightDealerHash = bmp.GetPixel(dataBag.RightDealerLocation.X, dataBag.RightDealerLocation.Y).ToArgb();
            pg.Refresh();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            table = Utility.FindWindow(txtTableName.Text);
            if (table != null)
            {

                btnSave.Enabled = true;
                btnGetImages.Enabled = true;
                btnSetFold.Enabled = true;
                btnConnect.Enabled = false;
                dataBag.TableSize = new Size(table.Rect.Width, table.Rect.Height);
                lblStatus.Text = "Connected!";
                pg = new PropertyGrid();
                pg.Dock = DockStyle.Fill;
                editorPanel.Controls.Add(pg);
                pg.SelectedObject = dataBag;
            }
            else
            {
                lblStatus.Text = "Can't find the window!";
            }

        }

        private void btnGetHoleCards_Click(object sender, EventArgs e)
        {
            if (dataBag.HoleCardSize.Width == 0)
            {
                MessageBox.Show("Please enter meta first!");
                return;
            }
            if (dataBag.HoleCardHashes == null)
            {
                dataBag.HoleCardHashes = new Dictionary<int, string>();
            }
            while (dataBag.HoleCardHashes.Count < 820)
            {
                Bitmap bmp = Utility.CaptureWindow(table);
                for (int i = 0; i < 2; i++)
                {
                    Bitmap card = Utility.GetRegion(bmp, new Rectangle(dataBag.LeftCards[i], dataBag.HoleCardSize));
                    int key = Utility.GetRegionHash(card, new Rectangle(new Point(0, 0), card.Size));
                    if (!dataBag.HoleCardHashes.ContainsKey(key))
                    {
                        dataBag.HoleCardHashes.Add(key, string.Empty);
                        if (!Directory.Exists(Directory.GetCurrentDirectory() + "\\images\\HoleCards"))
                        {
                            Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\images\\HoleCards");
                        }
                        string fileName = Directory.GetCurrentDirectory() + "\\images\\HoleCards\\" + DateTime.Now.Ticks.ToString() + ".bmp";
                        card.Save(fileName);
                    }
                }
                for (int i = 0; i < 2; i++)
                {
                    Bitmap card = Utility.GetRegion(bmp, new Rectangle(dataBag.RightCards[i], dataBag.HoleCardSize));
                    int key = Utility.GetRegionHash(card, new Rectangle(new Point(0, 0), card.Size));
                    if (!dataBag.HoleCardHashes.ContainsKey(key))
                    {
                        dataBag.HoleCardHashes.Add(key, string.Empty);
                        if (!Directory.Exists(Directory.GetCurrentDirectory() + "\\images\\HoleCards"))
                        {
                            Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\images\\HoleCards");
                        }
                        string fileName = Directory.GetCurrentDirectory() + "\\images\\HoleCards\\" + DateTime.Now.Ticks.ToString() + ".bmp";
                        card.Save(fileName);
                    }
                }
                lblStatus.Text = dataBag.HoleCardHashes.Count.ToString() + " of 57 completed";
                Thread.Sleep(2000);
            }
            lblStatus.Text = "Image creation finished!";

        }

        private void btnGetRightDealer_Click(object sender, EventArgs e)
        {
            Bitmap bmp = Utility.CaptureWindow(table);
            dataBag.RightDealerHash = bmp.GetPixel(dataBag.RightDealerLocation.X, dataBag.RightDealerLocation.Y).ToArgb();
            pg.Refresh();
        }

        private void btnGetLeftDealer_Click(object sender, EventArgs e)
        {
            Bitmap bmp = Utility.CaptureWindow(table);
            dataBag.LeftDealerHash = bmp.GetPixel(dataBag.LeftDealerLocation.X, dataBag.LeftDealerLocation.Y).ToArgb();
            pg.Refresh();
        }

        private void btnFoldButtonHashes_Click(object sender, EventArgs e)
        {
            Bitmap bmp = Utility.CaptureWindow(table);
            dataBag.FoldButtonHash = Utility.GetRegionHash(bmp, dataBag.FoldButtonRect);
            pg.Focus();
            pg.Refresh();
        }

        private void btnCheckButtonHash_Click(object sender, EventArgs e)
        {
            Bitmap bmp = Utility.CaptureWindow(table);
            dataBag.CheckButtonHash = Utility.GetRegionHash(bmp, dataBag.CheckButtonRect);
            pg.Focus();
            pg.Refresh();
        }

        private void btnCallButtonHash_Click(object sender, EventArgs e)
        {
            Bitmap bmp = Utility.CaptureWindow(table);
            pg.Focus();
            dataBag.CallButtonHash = Utility.GetRegionHash(bmp, dataBag.CallButtonRect);
            pg.Refresh();
        }

        private void btnBetButtonHash_Click(object sender, EventArgs e)
        {
            Bitmap bmp = Utility.CaptureWindow(table);
            dataBag.BetButtonHash = Utility.GetRegionHash(bmp, dataBag.BetButtonRect);
            pg.Focus();
            pg.Refresh();
        }

        private void btnRaiseButtonHash_Click(object sender, EventArgs e)
        {
            Bitmap bmp = Utility.CaptureWindow(table);
            dataBag.RaiseButtonHash = Utility.GetRegionHash(bmp, dataBag.RaiseButtonRect);
            pg.Focus();
            pg.Refresh();
        }

        private void btnGetFontColor_Click(object sender, EventArgs e)
        {
            Bitmap bmp = Utility.CaptureWindow(table);
            dataBag.PotFontColor = bmp.GetPixel(dataBag.PotFontPixel.X, dataBag.PotFontPixel.Y).ToArgb();
            pg.Focus();
            pg.Refresh();
        }

        private void btnGetLeftFold_Click(object sender, EventArgs e)
        {
            Bitmap bmp = Utility.CaptureWindow(table);
            dataBag.LeftFoldHash = Utility.GetRegionHash(bmp, dataBag.LeftFoldRect);
            pg.Focus();
            pg.Refresh();
        }

        private void btnGetRightFold_Click(object sender, EventArgs e)
        {
            Bitmap bmp = Utility.CaptureWindow(table);
            dataBag.RightFoldHash = Utility.GetRegionHash(bmp, dataBag.RightFoldRect);
            pg.Focus();
            pg.Refresh();
        } 
    }
}
